package com.cyt.work.antiepidemic.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.*;
import com.cyt.sys.model.po.SysDictContent;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.antiepidemic.dao.AntiepidemicCommunityDao;
import com.cyt.work.antiepidemic.model.po.AntiepidemicCommunity;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicCommunityVo;
import com.cyt.work.antiepidemic.service.AntiepidemicCommunityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * e门通-社区信息Service层实现类
 *
 * @author rong yuan
 * @date 2020-2-12 1:46:52
 */
@Service("antiepidemicCommunityService")
public class AntiepidemicCommunityServiceImpl extends BaseServiceImpl<AntiepidemicCommunity> implements AntiepidemicCommunityService {

    @Resource
    private AntiepidemicCommunityDao antiepidemicCommunityDao;

    @Autowired
    public void setBaseDao(AntiepidemicCommunityDao antiepidemicCommunityDao) {
        this.baseDao = antiepidemicCommunityDao;
    }

    /**
     * 分页查询
     *
     * @param antiepidemicCommunityVo 接受参数对象
     * @param pageNum                 分页数
     * @param pageSize                每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 1:46:52
     */
    @Override
    public ReturnResult page(AntiepidemicCommunityVo antiepidemicCommunityVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<AntiepidemicCommunityVo> list = antiepidemicCommunityDao.selectByCondition(antiepidemicCommunityVo);
        // 需要url
        /*if (CollUtil.isNotEmpty(list)) {
            for (AntiepidemicCommunityVo a : list) {
                String url = ProProperties.getInstance().getBaseUrl() + "/weChat/oauth?applyId=" + a.getApplyId() + "&path=/phone/antiepidemic/index&pathParams=communityId*" + a.getId();
                a.setQrUrl(url);
            }
        }*/
        PageInfo<AntiepidemicCommunityVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }
    /**
     * list查询
     *
     * @param antiepidemicCommunityVo 接受参数对象
     * @return List<AntiepidemicCommunity>
     * @author yu shengqiang
     * @date 2020-2-12 14:42:35
     */
    @Override
    public List<AntiepidemicCommunity> findList(AntiepidemicCommunityVo antiepidemicCommunityVo) {
        Example example = new Example(AntiepidemicCommunity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if (StringUtils.isNotEmpty(antiepidemicCommunityVo.getApplyId())) {
            criteria.andEqualTo("applyId",antiepidemicCommunityVo.getApplyId());
        }
        return antiepidemicCommunityDao.selectByExample(example);
    }
    /**
     * 添加或更新
     *
     * @param antiepidemicCommunityVo 参数接受对象
     * @param sysUser                 当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 1:46:52
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(AntiepidemicCommunityVo antiepidemicCommunityVo, SysUser sysUser) {
        if (StringUtils.isBlank(antiepidemicCommunityVo.getId())) {
            AntiepidemicCommunity antiepidemicCommunity = new AntiepidemicCommunity(sysUser);
            antiepidemicCommunity.setApplyId(antiepidemicCommunityVo.getApplyId());
            antiepidemicCommunity.setProvinceCode(antiepidemicCommunityVo.getProvinceCode());
            antiepidemicCommunity.setCityCode(antiepidemicCommunityVo.getCityCode());
            antiepidemicCommunity.setRegionCode(antiepidemicCommunityVo.getRegionCode());
            antiepidemicCommunity.setBuildingName(antiepidemicCommunityVo.getBuildingName());
            antiepidemicCommunity.setMaxTripTimes(antiepidemicCommunityVo.getMaxTripTimes());
            antiepidemicCommunity.setCheckType(antiepidemicCommunityVo.getCheckType());
            antiepidemicCommunity.setCommunityType(antiepidemicCommunityVo.getCommunityType());
            antiepidemicCommunity.setGoStartWork(antiepidemicCommunityVo.getGoStartWork());
            antiepidemicCommunity.setGoEndWork(antiepidemicCommunityVo.getGoEndWork());
            antiepidemicCommunity.setOffStartWork(antiepidemicCommunityVo.getOffStartWork());
            antiepidemicCommunity.setOffEndWork(antiepidemicCommunityVo.getOffEndWork());
            antiepidemicCommunityDao.insert(antiepidemicCommunity);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            AntiepidemicCommunity antiepidemicCommunity = antiepidemicCommunityDao.selectByPrimaryKey(antiepidemicCommunityVo.getId());
            antiepidemicCommunity.setApplyId(antiepidemicCommunityVo.getApplyId());
            antiepidemicCommunity.setProvinceCode(antiepidemicCommunityVo.getProvinceCode());
            antiepidemicCommunity.setCityCode(antiepidemicCommunityVo.getCityCode());
            antiepidemicCommunity.setRegionCode(antiepidemicCommunityVo.getRegionCode());
            antiepidemicCommunity.setBuildingName(antiepidemicCommunityVo.getBuildingName());
            antiepidemicCommunity.setMaxTripTimes(antiepidemicCommunityVo.getMaxTripTimes());
            antiepidemicCommunity.setCheckType(antiepidemicCommunityVo.getCheckType());
            antiepidemicCommunity.setCommunityType(antiepidemicCommunityVo.getCommunityType());
            antiepidemicCommunity.setGoStartWork(antiepidemicCommunityVo.getGoStartWork());
            antiepidemicCommunity.setGoEndWork(antiepidemicCommunityVo.getGoEndWork());
            antiepidemicCommunity.setOffStartWork(antiepidemicCommunityVo.getOffStartWork());
            antiepidemicCommunity.setOffEndWork(antiepidemicCommunityVo.getOffEndWork());
            antiepidemicCommunity.setUpdateTime(new Date());
            antiepidemicCommunity.setUpdateUser(sysUser.getId());
            antiepidemicCommunityDao.updateByPrimaryKey(antiepidemicCommunity);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param antiepidemicCommunityVo 参数接受对象
     * @param sysUser                 当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 1:46:52
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(AntiepidemicCommunityVo antiepidemicCommunityVo, SysUser sysUser) {
        //要修改的字段
        AntiepidemicCommunity antiepidemicCommunity = new AntiepidemicCommunity();
        antiepidemicCommunity.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        antiepidemicCommunity.setUpdateUser(sysUser.getId());
        antiepidemicCommunity.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(AntiepidemicCommunity.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(antiepidemicCommunityVo.getId())) {
            criteria.andIn("id", Arrays.asList(antiepidemicCommunityVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        antiepidemicCommunityDao.updateByExampleSelective(antiepidemicCommunity, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param antiepidemicCommunityVo 参数接受对象
     * @param sysUser                 当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 1:46:52
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(AntiepidemicCommunityVo antiepidemicCommunityVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(AntiepidemicCommunity.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(antiepidemicCommunityVo.getId())) {
            criteria.andIn("id", Arrays.asList(antiepidemicCommunityVo.getId().split(",")));
        }
        antiepidemicCommunityDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    public ReturnResult acceptSelect(String code) {
        List<AntiepidemicCommunityVo> list = antiepidemicCommunityDao.selectByCondition(new AntiepidemicCommunityVo());
        List<SysDictContent> sysDictContentList = new ArrayList<SysDictContent>();
        for (AntiepidemicCommunityVo entity : list) {
            SysDictContent sysDictContent = new SysDictContent();
            sysDictContent.setId(entity.getId());
            sysDictContent.setName(entity.getBuildingName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(entity.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnResult(sysDictContentList);
    }
}